package model.posterminal;

import java.util.List;

import model.configuration.ConfigData;

public class PosterminalBusinessRules {

	private static final Posterminal servicePosterminal = new Posterminal();
	
	/**
	 * Check comprehensiveness of posterminal data
	 * @param posterminal
	 * @return
	 * @throws Exception 
	 */
	public static boolean checkComprehensivenessData(Posterminal posterminal) throws Exception {
		
		boolean result = false;
		List<Posterminal> posList = servicePosterminal.getAllPosterminalList();
		//1 model must be in database
		if (posterminal.getId()!= null && posterminal.getPosterminalModel().getId()!=ConfigData.NOVALUELONG) { 
			//2 serial number must exist
			if ((posterminal.getSerialNumber() != null) 
					&& (posterminal.getSerialNumber().length()>=ConfigData.MIN_SERIAL_NUMBER_LENGTH)) {
				//3 check uniqueness for serial and inventory number if another posterminals already exists
				if (posList.size()>0) {
					result = true; // think that there is no doubles.
					for (Posterminal existPos : posList) {
						// uniqueness serial number
						if (posterminal.getSerialNumber().equalsIgnoreCase(existPos.getSerialNumber())
								// and inventory number uniqueness
								|| ((posterminal.getInventoryNumber().equalsIgnoreCase(existPos.getInventoryNumber())
										// or inventory number is empty
										&& !(posterminal.getInventoryNumber() == null || posterminal.getInventoryNumber().length()<=ConfigData.ZERO_STRING_SIZE)))
										//|| (posterminal.getInventoryNumber() == null || posterminal.getInventoryNumber().length()<=ConfigData.MIN_SERIAL_NUMBER_LENGTH
										)
										{
							result = false;
							break;
						} 
					}
					
				} else {
					result = true; // this is a fist posterminal
				}
			} else {
				result = false;
			}
		}
		return result;
	}
	
}
